package content

import (
	"context"
	"fmt"
	"time"

	"gddgame.cc/galaxy/common/namespaces/cooperate"
	"gddgame.cc/galaxy/project/breeder/breeder/cooperated"
	"gddgame.cc/galaxy/utils"
)

type AutoContent struct {
	*cooperated.TypeMap
}

func (content *AutoContent) OnStart(master bool, proxy cooperate.MapProxy, context context.Context, data cooperate.PropsInstance) error {
	if !master {
		return nil
	}
	center := proxy.GetMapData().Center
	ring := proxy.RingDistance(center, 5)
	positions := [][]byte{}
	for i := 0; i < len(ring); i += 3 {
		positions = append(positions, ring[i])
	}
	//fmt.Println("auto", positions)
	tt := time.NewTicker(time.Minute)
	go func() {
		for {
			select {
			case <-tt.C:
				//fmt.Println("auto ticker")
				_, itemList, identityList := proxy.GetItemList(positions, proxy.CurrentTime())
				//fmt.Println(itemList, identityList)
				i := 0
				for index, item := range itemList {
					position := positions[index]
					if item == nil {
						//fmt.Println("auto query", position)
						content.OnQuery(proxy, 0, 0, cooperated.CreateUnit, []interface{}{"ai", position, "ai" + utils.ToString(index)})
						continue
					}
					identity := identityList[index]
					if identity > 0 {
						continue
					}
					i += 1
				}
				//fmt.Println("auto ai number", i)
			case <-context.Done():
				fmt.Println("auto stop")
				tt.Stop()
				return
			}
		}

	}()
	return nil
}
func (content *AutoContent) OnStop() {

}

// 自定义定时器
func (content *AutoContent) Execute(task string) {

}
